package com.cloudcc.cloudframe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.cloudcc.cloudframe.util.ListUtils;
import com.litesuits.android.log.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author wangzhiqiang
 * @date 2015-7-10 上午9:17:48
 * 
 */
public abstract class NewCommonAdapter<T> extends BaseAdapter {

	protected List<T> mData;
	protected Context mContext;
	protected LayoutInflater mInflater;

	public NewCommonAdapter(Context mContext) {
		super();
		mData = new ArrayList<T>();
		this.mContext = mContext;
		this.mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return ListUtils.getSize(mData);
	}

	@Override
	public T getItem(int position) {
		return mData.get(position);
	}

	/**
	 * 添加数据集
	 * 
	 * @param data
	 */
	public void addData(List<T> data) {
		if (mData == null) {
			mData = new ArrayList<T>();
		}
		mData.addAll(data);
		notifyDataSetChanged();
		Log.d("istrue", "过来了");
	}

	/**
	 * 添加单条数据
	 * 
	 * @param t
	 */
	public void addItem(T t) {
		if (mData == null) {
			mData = new ArrayList<T>();
		}
		mData.add(t);
		notifyDataSetChanged();
	}

	/**
	 * 在指定位置添加一条数据
	 * 
	 * @param pos
	 * @param obj
	 */
	public void addItem(int pos, T obj) {
		if (mData == null) {
			mData = new ArrayList<T>();
		}
		mData.add(pos, obj);
		notifyDataSetChanged();
	}

	/**
	 * 移除一条数据
	 * 
	 * @param obj
	 */
	public void removeItem(T obj) {
		mData.remove(obj);
		notifyDataSetChanged();
	}

	/**
	 * 移除一条记录
	 * 
	 * @param pos
	 */
	public void removeItem(int pos) {
		mData.remove(pos);
		notifyDataSetChanged();
	}

	/**
	 * 清空数据
	 */
	public void clear() {
		if (mData != null) {
			mData.clear();
		}
		notifyDataSetChanged();
	}

	public void changeData(List<T> data) {
		if (mData != null) {
			mData.clear();
			mData = null;
		}
		mData = data;
		Log.d("isnull", "is:" + mData != null + "::" + data.toString() + "::"
				+ (data != null));
		notifyDataSetChanged();
	}

	/**
	 * @return
	 */
	public List<T> getData() {
		return mData;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = ViewHolder.get(mContext, convertView, parent,
				getLayoutId(), position);
		convert(holder, getItem(position),position);
		return holder.getConvertView();
	}

	/**
	 * 将view的数据跟view绑定
	 * 
	 * @param holder
	 * @param t
	 */
	public abstract void convert(ViewHolder holder, T t,int position);

	/**
	 * 获得布局
	 * 
	 * @return
	 */
	public abstract int getLayoutId();

}
